package com.homedo.odin.solution.service.bizservice.personal;

import com.github.pagehelper.PageInfo;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.ObjectConverter;
import com.homedo.odin.solution.contract.pc.GetMyMsgResp;
import com.homedo.odin.solution.contract.pc.InsertMsgReq;
import com.homedo.odin.solution.contract.pc.MsgTemplateResp;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.Message;
import com.homedo.odin.solution.model.entity.MessageType;
import com.homedo.odin.solution.model.entity.UserBasic;
import com.homedo.odin.solution.service.MessageServiceImpl;
import com.homedo.odin.solution.service.MessageTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: donglibing
 * @date: 2018/3/7 18:58
 * @Description: 信息业务处理
 */
@Service
public class MessageBizService {

    @Autowired
    MessageServiceImpl impl;

    @Autowired
    MessageTypeServiceImpl typeImpl;

    @Autowired
    CommonBizService commonBizService;

    /**
     * 获取我的信息
     */
    public GetMyMsgResp getMessageList(int pageIndex, int pageSize, int userId, int readed) {
        GetMyMsgResp resp = new GetMyMsgResp();
        PageBean pageBean = new PageBean(pageIndex, pageSize);
        PageInfo<Message> messageList = impl.getMessageList(pageBean, userId, readed);
        resp = BeanUtils.transfrom(GetMyMsgResp.class, messageList);
        return resp;
    }

    /**
     * 删除信息
     */
    public void deleteMessage(String ids, int userId) {
        //判断UserId是否存在
        UserBasic userBasic = commonBizService.validationUser(userId);
        impl.deleteMessage(ids, userId);
    }

    /**
     * 标记为已读
     */
    public void readMessage(String ids, int userId) {
        //判断UserId是否存在
        UserBasic userBasic = commonBizService.validationUser(userId);
        impl.readMessage(ids, userId);
    }

    /**
     * 新增消息
     */
    public void insertMessage(InsertMsgReq req) {
        //数据校验
        //判断UserId是否存在
        commonBizService.validationUser(req.getToUser());

        commonBizService.sentMessage(req.getContentMap(), req.getToUser(), req.getFromeUser(), req.getCode());

    }

    /**
     * 获取信息模板
     */
    public MsgTemplateResp getMessageTemplate(String code) {
        MsgTemplateResp resp = new MsgTemplateResp();
        MessageType messageType = typeImpl.getMessageTemplate(code);
        ObjectConverter.copyProperties(messageType, resp);
        return resp;
    }

}
